//
//  UIScrollView+Refresh.swift
//  Risac
//
//  Created by remy on 2018/5/6.
//

import UIKit

extension UIScrollView: RisacCompatible {}

internal extension UIImage {
    
    static func resource(name: String) -> UIImage? {
        return UIImage(named: name, in: getResourceBundle(), compatibleWith: nil)
    }
    
    static func getResourceBundle() -> Bundle? {
        let bundle = Bundle(for: RefreshComponent.self)
        if let bundle = bundle.path(forResource: "Refresh", ofType: "bundle") {
            return Bundle(path: bundle)
        }
        return bundle
    }
}

extension RisacSpace where Base: UIScrollView {
    
    public var refreshHeader: RefreshComponent? {
        get {
            return objc_getAssociatedObject(base, AssociationKey.make(#function)) as? RefreshComponent
        }
        set {
            objc_setAssociatedObject(base, AssociationKey.make(#function), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    public var refreshFooter: RefreshComponent? {
        get {
            return objc_getAssociatedObject(base, AssociationKey.make(#function)) as? RefreshComponent
        }
        set {
            objc_setAssociatedObject(base, AssociationKey.make(#function), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    private func addRefresh<T: RefreshComponent>(_ actionHandler: @escaping () -> Void, cls: T.Type) -> T {
        base.isMultipleTouchEnabled = false
        base.panGestureRecognizer.maximumNumberOfTouches = 1
        let view = cls.init()
        view.actionHandler = actionHandler
        base.addSubview(view)
        return view
    }
    
    @discardableResult
    public func addTopRefresh(_ actionHandler: @escaping () -> Void) -> TopRefreshView {
        let r = addRefresh(actionHandler, cls: TopRefreshView.self)
        r.animator = RefreshComponent.defaultPullAnimator.init()
        removeRefreshHeader()
        refreshHeader = r
        return r
    }
    
    @discardableResult
    public func addTopAutoRefresh(_ actionHandler: @escaping () -> Void) -> TopAutoRefreshView {
        let r = addRefresh(actionHandler, cls: TopAutoRefreshView.self)
        r.animator = RefreshComponent.defaultAutoAnimator.init()
        removeRefreshHeader()
        refreshHeader = r
        return r
    }
    
    @discardableResult
    public func addBottomRefresh(_ actionHandler: @escaping () -> Void) -> BottomRefreshView {
        let r = addRefresh(actionHandler, cls: BottomRefreshView.self)
        r.animator = RefreshComponent.defaultPullAnimator.init(isTop: false)
        removeRefreshFooter()
        refreshFooter = r
        return r
    }
    
    @discardableResult
    public func addBottomAutoRefresh(_ actionHandler: @escaping () -> Void) -> BottomAutoRefreshView {
        let r = addRefresh(actionHandler, cls: BottomAutoRefreshView.self)
        r.animator = RefreshComponent.defaultAutoAnimator.init(isTop: false)
        removeRefreshFooter()
        refreshFooter = r
        return r
    }
    
    public func removeRefreshHeader() {
        guard let refreshHeader = refreshHeader else { return }
        refreshHeader.stopRefreshing()
        refreshHeader.removeFromSuperview()
        self.refreshHeader = nil
    }
    
    public func removeRefreshFooter() {
        guard let refreshFooter = refreshFooter else { return }
        refreshFooter.stopRefreshing()
        refreshFooter.removeFromSuperview()
        self.refreshFooter = nil
    }
}
